/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pkg4funparse;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import pkg4fungeneric.Code;
import pkg4funparse.Symbol.Types;

/**
 *
 * @author l
 */
class Input {
    private static ArrayList<Integer> rules[];
    private static ArrayList<Symbol> symbols;
    private static int[][] M;
    private static Object sim;

    public static void init(String gramar_rules) throws FileNotFoundException, IOException {
        String buf;
        BufferedReader inFile = new BufferedReader(new FileReader(gramar_rules));
        HashMap<String, Integer> tokenIndex = new HashMap<String, Integer>();
        HashMap<String, Symbol> tokenSymbol = new HashMap<String, Symbol>();
        symbols = new ArrayList<Symbol>();
        buf = inFile.readLine();
        Data.setN_TERM_SIZE(Integer.parseInt(buf));
//    após encher o vetor de simbolos os não terminais ocuparão do 0 à
//    | nT | - 1, os terminais de | nT | até | nT | + | T | - 1.
        for (int i = 0; i < Data.N_TERM_SIZE; i++)
        {
            buf = inFile.readLine().trim();
            Symbol s = new Symbol(Types.N_TERMINAL, buf);
            symbols.add(s);
            Symbol t = symbols.get(symbols.indexOf(s));
            System.out.println("symbols.get("+symbols.indexOf(s)+").value = "+ symbols.get(symbols.indexOf(s)).value);
            tokenSymbol.put(buf, s);
        }
        buf = inFile.readLine();
        int termSize = Integer.parseInt(buf);
        Data.setTermSize(termSize);
        System.out.println("TERM_SIZE: " + Data.TERM_SIZE);
        int termMax = Data.N_TERM_SIZE + termSize;
        for(int i = Data.N_TERM_SIZE; i < termMax; i++)
        {
            buf = inFile.readLine().trim();
            Symbol s = new Symbol(Types.TERMINAL, buf);
            symbols.add(s);
            tokenIndex.put(buf, i);
            tokenSymbol.put(buf,s);
        }
        Data.setTki(tokenIndex);
        Data.setTki(tokenSymbol);
        buf = inFile.readLine();
        // Para o vetor de regras, os valores da tabela M do preditivo são
        // índices de ArrayLists de codigos de simbolos equivalentes aos simbolos
        // da cadeia derivada, esses codigos por sua vez são indices para o vetor
        // de simbolos.
        int rulesSize = Integer.parseInt(buf);
        @SuppressWarnings("unchecked")
        ArrayList<Integer> Rs [] = new ArrayList[rulesSize];
        for(int i = 0; i < rulesSize; i++) {
            Rs[i] = new ArrayList<Integer>();
        }
        rules = Rs;
        System.out.println("Rulesize: " + rulesSize);
        for(int i = 0; i <  rulesSize; i++)
        {
            ArrayList<Integer> a = new ArrayList<Integer>();
            buf = inFile.readLine().trim();
            for(String s : buf.split(" "))
            {
                a.add(Integer.valueOf(s));
            }
            rules[i] = a;
        }

        M = new int[Data.N_TERM_SIZE][Data.TERM_SIZE];
        for(int i = 0; i < Data.N_TERM_SIZE; i++)
            for(int j = 0; j < termSize; j++)
                M[i][j] = Code.ERRO;

        buf = inFile.readLine();
        while(buf != null) {
            buf.trim();
            String n[] = buf.split(" ");
            int lin = Integer.parseInt(n[0]);
            int col = Integer.parseInt(n[1]) - Data.N_TERM_SIZE;
            int res = Integer.parseInt(n[2]);
            if(lin>=Data.N_TERM_SIZE || col>=Data.TERM_SIZE)
                System.out.println("N_TERMSIZE:"+Data.N_TERM_SIZE+"term: "+symbols.get(Integer.valueOf(n[1])).value+" "+lin+":"+Data.N_TERM_SIZE+" "+col+":"+Data.TERM_SIZE);
            M[lin][col] = res;
            buf = inFile.readLine();
        }
//
//        for(int i = 0; i < Data.N_TERM_SIZE; i++) {
//            for(int j = 0; j < Data.TERM_SIZE; j++) {
//                Symbol sim1 = (Symbol) symbols.get(i);
//                Symbol sim2 = (Symbol) symbols.get(Data.code(j));
//                System.out.print("M[" + sim1.value + "][" + sim2.value + "] = ");
//
//        System.out.println(i + " " + Data.TERM_SIZE);
//                if(M[i][j] == -1) System.out.println("Erro");
//                else {
//
//                    System.out.println("MAXXXXXXXXXXXXXXXX " + M[i][j] + " " + i + " " + j);
//                    int max = rules[M[i][j]].size();
//                    for(int k = 0; k < max; k++) {
//                        int cod = (Integer) rules[M[i][j]].get(k);
//                        Symbol symb = (Symbol) symbols.get(cod);
//                        System.out.print(symb.type + " ");
//                        //symbols.get(cod).value
//                    }
//                }
//                System.out.println();
//            }
//        }
        Data.setRules(rules);
        Data.setSymbols(symbols);
        Data.setM(M);
        Data.setTermSize(termSize);
        Data.setTki(tokenIndex);
        Data.setTks(tokenSymbol);
    }

}
